/*
 * Copyright 2005-2013 biz-united. All rights reserved.
 * Support: http://www.biz-united.com.cn
 * License: http://www.biz-united.com.cn/?page_id=5&act=3
 */
package net.biz.dao.impl;

import net.biz.Page;
import net.biz.Pageable;
import net.biz.entity.Admin;
import net.biz.entity.AdminRecord;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;
import java.util.List;

@Repository("recordDaoImpl")
public class RecordDaoImpl extends BaseDaoImpl<AdminRecord, Long> {

	public Page<AdminRecord> findPage(Admin admin, Date beginDate, Date endDate,Pageable pageable) {
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<AdminRecord> criteriaQuery = criteriaBuilder.createQuery(AdminRecord.class);
		Root<AdminRecord> root = criteriaQuery.from(AdminRecord.class);
		criteriaQuery.select(root);
		Predicate restrictions = criteriaBuilder.conjunction();

		if(admin!=null){
			restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("admin"), admin));
		}
		if(beginDate!=null){
			restrictions = criteriaBuilder.and(restrictions,root.get("createDate").isNotNull(), criteriaBuilder.greaterThanOrEqualTo(root.<Date> get("createDate"), beginDate));
		}
		if(endDate!=null){
			restrictions = criteriaBuilder.and(restrictions, root.get("createDate").isNotNull(),criteriaBuilder.lessThanOrEqualTo(root.<Date> get("createDate"), endDate));
		}
		criteriaQuery.where(restrictions);
		return super.findPage(criteriaQuery, pageable);
		
	}

	public List<AdminRecord> getRecordList(Admin admin, Date beginDate, Date endDate, Pageable pageable) {
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<AdminRecord> criteriaQuery = criteriaBuilder.createQuery(AdminRecord.class);
		Root<AdminRecord> root = criteriaQuery.from(AdminRecord.class);
		criteriaQuery.select(root);
		Predicate restrictions = criteriaBuilder.conjunction();

		if(admin!=null){
			restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("admin"), admin));
		}
		if(beginDate!=null){
			restrictions = criteriaBuilder.and(restrictions,root.get("createDate").isNotNull(), criteriaBuilder.greaterThanOrEqualTo(root.<Date> get("createDate"), beginDate));
		}
		if(endDate!=null){
			restrictions = criteriaBuilder.and(restrictions, root.get("createDate").isNotNull(),criteriaBuilder.lessThanOrEqualTo(root.<Date> get("createDate"), endDate));
		}
		criteriaQuery.where(restrictions);
		return super.findList(criteriaQuery,0,60000,null,null);

	}

}